package com.collect.cameroon.zhiScene.zhiAuditer.zhiFollow.zhiCollect;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.collect.cameroon.R;
import com.collect.cameroon.zhiAdater.zhiDaily.ZHIDailyListAdjuster;
import com.collect.cameroon.zhiBase.BaseFragment;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRCaseEntity;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRCaseListEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISDailyCaseEntity;
import com.collect.cameroon.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.cameroon.zhiHelper.ZHIMainProcessHelper;
import com.collect.cameroon.zhiScene.zhiAuditer.zhiFollow.ZHIAuditerFollowFragment;
import com.collect.cameroon.zhiScene.zhiAuditer.zhiRoot.ZHIRootAuditerFragmentInterface;
import com.collect.cameroon.zhiScene.zhiWidget.zhiNavigationBar.ZHINavigationBar;

import java.util.ArrayList;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class ZHIAuditerCollectFragment extends BaseFragment<ZHIAuditerCollectPresenter> implements ZHIAuditerCollectContract.View {

    @BindView(R.id.navigationBar)
    ZHINavigationBar navigationBar;

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.rc_list)
    RecyclerView rc_list;

    @BindView(R.id.iv_filter)
    ImageView iv_filter;

    @BindView(R.id.iv_words)
    ImageView iv_words;


    private static int mTotalCount = 0;

    private final ZHIDailyListAdjuster mZHIDailyListAdjuster = new ZHIDailyListAdjuster();

    private ArrayList<ZHIRCaseEntity> mDailyEntityArrayList;

    private ZHIRootAuditerFragmentInterface mZHIRootAuditerFragmentInterface;

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_auditer_collect;
    }

    @Override
    protected void initView(View view) {
        srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));
        iv_words.setOnClickListener(v -> ZHIProjectRouter.toWordsSkills());
//        try {
//            mZHIRootAuditerFragmentInterface = (ZHIRootAuditerFragmentInterface) getParentFragment();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        initData(true);
        initEvent();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            mZHIRootAuditerFragmentInterface = (ZHIRootAuditerFragmentInterface) activity;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        if (ZHIMainProcessHelper.getInstance().isHadNewCaseRecord()) {
            ZHIMainProcessHelper.getInstance().setHadNewCaseRecord(false);
            initData(true);
        }
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onGetLatestCaseListV2(ZHIRCaseListEntity caseListEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            mDailyEntityArrayList = caseListEntity.getDunList();
            mZHIDailyListAdjuster.setNewData(mDailyEntityArrayList);

            mTotalCount = mDailyEntityArrayList.size();

            String narText = "Total: " + mTotalCount;
//            tv_page_title.setText(narText);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("InflateParams")
    public void initEvent() {
        try {
            srl_swipe_layout.setOnRefreshListener(() -> initData(false));

            // 导航栏
            navigationBar.hideLeft().setCenterText(getString(R.string.tab_daily));

            mZHIDailyListAdjuster.setEmptyView(getRefreshEmptyView());
            mZHIDailyListAdjuster.setOnOrderItemClickListener(caseEntity -> ZHIProjectRouter.toDetailV2(caseEntity.getUserId(), caseEntity.getOrderId(), caseEntity.getAppId()));

            rc_list.setLayoutManager(new LinearLayoutManager(this.getActivity()));
            rc_list.setAdapter(mZHIDailyListAdjuster);

            iv_filter.setOnClickListener(v -> {
                if (mZHIRootAuditerFragmentInterface != null) {
                    mZHIRootAuditerFragmentInterface.onShowFilterDaily();
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int dataCount() {
        return mTotalCount;
    }

    public void initData(boolean showDialog) {
        try {
            ZHISDailyCaseEntity zhisDailyCaseEntity = getCaseSearchParams();
            if (zhisDailyCaseEntity == null) {
                srl_swipe_layout.setRefreshing(false);
                return;
            }

            assert mPresenter != null;
            mPresenter.getLatestCaseListV2(getActivity(), zhisDailyCaseEntity, showDialog);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void refreshData(ZHISDailyCaseEntity zhisDailyCaseEntity) {
        if (mPresenter != null && zhisDailyCaseEntity != null) {
            mPresenter.getLatestCaseListV2(getActivity(), zhisDailyCaseEntity, true);
        }
    }

    public void refreshData() {
        initData(true);
    }

    private ZHISDailyCaseEntity getCaseSearchParams() {
        if (mZHIRootAuditerFragmentInterface != null) {
            return mZHIRootAuditerFragmentInterface.onGetDailyFilterParams();
        }
        return null;
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> {
            initData(true);
        });
        return emptyView;
    }
}
